﻿using System;
using System.Data;
using System.Linq;
using Model;

namespace BLL
{
    public class Information_Content_BLL
    {
        ExtendedDBModelContainer _db = new ExtendedDBModelContainer();

        public IQueryable<Information_Content> GetValid()
        {
            var model = Get().Where(a => a.Deleted.Equals(false));
            return model;
        }

        public IQueryable<Information_Content> Get()
        {
            var model = _db.Information_Content.OrderByDescending(a => a.IsTop).ThenByDescending(a => a.AddDate);
            return model;
        }

        public Information_Content Get(Guid? Id)
        {
            var item = new Information_Content();
            item.AddDate = DateTime.Now;
            if (Id != null)
            {
                item = _db.Information_Content.Single(a => a.Id.Equals(Id.Value));
            }
            item.LastEditDate = DateTime.Now;
            return item;
        }

        public void Save(Guid? Id, ref Information_Content item)
        {
            ExtendedDBModelContainer _db = new ExtendedDBModelContainer();

            if (Id == null)
            {
                _db.Information_Content.Attach(item);
                _db.ObjectStateManager.ChangeObjectState(item, EntityState.Added);
            }
            else
            {
                _db.Information_Content.Attach(item);
                _db.ObjectStateManager.ChangeObjectState(item, EntityState.Modified);
            }

            _db.SaveChanges();
        }

        public void DeleteObject(Guid Id)
        {
            var item = new Information_Content() { Id = Id };
            _db.Information_Content.Attach(item);
            _db.ObjectStateManager.ChangeObjectState(item, EntityState.Deleted);
            _db.SaveChanges();
        }
    }
}


